package com.tc.dispatch.csservice.ejb;

import java.util.*;
import javax.ejb.*;

import com.tc.dispatch.csservice.exception.*;
import com.tc.dispatch.csservice.model.*;
import com.tc.dispatch.csservice.util.*;
import com.tc.dispatch.gsservice.ejb.*;

import com.tc.dispatch.gsservice.exception.GSException;
import com.tc.dispatch.gsservice.action.GSSimilary;
import com.tc.dispatch.gsalgorithm.spec.parse;
import com.tc.dispatch.gsservice.model.OrderSumMain;
import com.tc.dispatch.csservice.algorithm.CostAlgorithm;
import com.tc.dispatch.gsalgorithm.exception.AlgorithmException;
import com.tc.dispatch.csservice.util.OrderSimComp;
import com.tc.dispatch.csservice.util.widgt.WidgtFactory;
import com.tc.dispatch.csservice.util.widgt.ComponentFactory;

import com.tc.dispatch.csservice.filter.ComponentFilter;
import com.tc.dispatch.csservice.algorithm.adjust.Adjust;

import com.tc.dispatch.csservice.algorithm.calculation.Calclation;
import com.tc.dispatch.csservice.algorithm.gpa.GSGPA;
import com.tc.expertsystem.es.ejb.ESServiceLocal;
import com.tc.expertsystem.es.ejb.ESServiceLocalHome;
import com.tc.dispatch.csservice.algorithm.bp.BPInterface;
import com.tc.dispatch.csservice.algorithm.bp.BPImplement;
import com.tc.expertsystem.es.exception.*;

public class CSServiceBean
    implements SessionBean {
  SessionContext sessionContext;

  GSServiceLocal gsservicelocal;
  com.tc.dispatch.csservice.dao.MaoPiDAO maopiservicedao;
  com.tc.dispatch.csservice.dao.ProcsDetailDAO procsdetaildao;
  com.tc.dispatch.csservice.dao.ManuCostDAO manucostdao;
  CostAlgorithm ca;
  Calclation calcAll, calcMin;
  ESServiceLocal esServiceLocal = null;
  BPInterface bp;

  public void ejbCreate() throws CreateException {

    ServiceLocator sl = new ServiceLocator();
    maopiservicedao = DAOFactory.getMaoPiServiceDAO();
    gsservicelocal = ( (GSServiceLocalHome) sl.getEJBLocalHome(JNDINames.
	GSSERVICE_EJB_LOCAL
	)).create();
    esServiceLocal = ((ESServiceLocalHome) sl.getEJBLocalHome(JNDINames.
	ESSERVICE_EJB_LOCAL)).create();
    procsdetaildao = DAOFactory.getProcsDetailDAO();
    manucostdao = DAOFactory.getManuCostDAO();
    ca = AlgorithmFactory.getCostAlgorithm();
    calcAll = AlgorithmFactory.getCalclationAll();
    calcMin = AlgorithmFactory.getCalclationYield();

    bp=AlgorithmFactory.getBPAlgorithm(esServiceLocal);
    calcAll.initConfig(bp,gsservicelocal);
    calcMin.initConfig(bp,gsservicelocal);
    ca.setCalc(calcAll);
    ca.setCalcYield(calcMin);


    //gsHelper.setGSServiceLocal(gsservicelocal);
    //System.out.println("---------ejb:"+gsHelper.getGSServiceLocal());
  }

  public void ejbRemove() {

  }

  public void ejbActivate() {

  }

  public void ejbPassivate() {

  }

  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }

  public void createMaoPi(MaoPiDanJia maopi) throws CSException {
    maopiservicedao.createMaoPi(maopi);
  }

  public void updateMaoPi(MaoPiDanJia maopi) throws CSException {
    maopiservicedao.updateMaoPi(maopi);
  }

  public void deleteMaoPi(String ssign) throws CSException {
    maopiservicedao.deleteMaoPi(ssign);
  }

  public MaoPiDanJia getMaoPi(String ssign) throws CSException {

    return maopiservicedao.getMaoPi(ssign);
  }

  public MaoPiDanJia findMaoPi(String wherestring) throws CSException {
    /**@todo Complete this method*/
    return maopiservicedao.findMaoPi(wherestring);
  }

  public List findAllMaoPis() throws CSException {
    /**@todo Complete this method*/
    return maopiservicedao.findAllMaoPis();
  }

  public List findUndoBaoJia() throws CSException {
    /**@todo Complete this method*/
    return null;
  }

  public double getSimilarCost(String ssign, String spec, int fac) throws CSException {
    return 0;
  }

  public double generateEstimateCost(Product p) {

    return 0;
  }

  public int getMPTotalNums(String whereStr) throws CSException {
    /**@todo Complete this method*/
    return maopiservicedao.getMPTotalNums(whereStr);
  }

  public List findMaoPiByPagesWhereStr(int start, int limit, String whereStr) throws CSException {
    /**@todo Complete this method*/
    return maopiservicedao.findMaoPiByPageWhere(start, limit, whereStr);
  }

  public void removeMaoPi(int id) throws CSException {
    /**@todo Complete this method*/
    maopiservicedao.removeMaoPi(id);
  }

  public void updateManuCost(ManuCost mc) throws CSException {
    /**@todo Complete this method*/
    //   System.out.println("enter into ejb");
    manucostdao.updateManuCost(mc);
  }

  public List findManuCost(String whereStr) throws CSException {
    /**@todo Complete this method*/
    return manucostdao.findManuCost(whereStr);
  }

  public List findMinManuCostByPage(int start, int limit, String whereStr) throws CSException {
    /**@todo Complete this method*/
    return manucostdao.findMinManuCostByPage(start, limit, whereStr);
  }

  public int getMinManuCostTotalNums(String whereStr) throws CSException {
    /**@todo Complete this method*/
    return manucostdao.getTotalNums(whereStr);
  }

  public void createProcsDetail(ProcsDetail pd) throws CSException {
    /**@todo Complete this method*/
    procsdetaildao.createProcsDetail(pd);
  }

  public void updateProcsDetail(ProcsDetail pd) throws CSException {
    /**@todo Complete this method*/
    procsdetaildao.updateProcsDetail(pd);
  }

  public void removeProcsDetail(ProcsDetail pd) throws CSException {
    /**@todo Complete this method*/
    procsdetaildao.removeProcsDetail(pd);
  }

  public List findProcsDetail(String whereStr) throws CSException {
    /**@todo Complete this method*/
    return procsdetaildao.findProcsDetail(whereStr);
  }

  public CostAlgorithm getCostAlgorithm() {
    /**@todo Complete this method*/
    return ca;
  }

  public void generateCaiLiaoFee() {
    /**@todo Complete this method*/
  }

  public void generateProcessFee() {
    /**@todo Complete this method*/
  }

  public void generateCaiLiaoFeeDiff() {
    /**@todo Complete this method*/
  }

  public void generateProcessFeeDiff() {
    /**@todo Complete this method*/
  }

  public List getSortedCombinedSimOrder(Product p) {
    /**@todo Complete this method*/
    try {
      String facStr = " and factory=" + p.getFactory();
      if (p.getFactory() == 0) {
	facStr = "";
      }
      List simList = gsservicelocal.findOrderSumMain(" where spectype='" +
	  p.getSpecType() + "' and processScheme=" + p.getScheme() +
	  " and type=1 " + facStr+" and product_cost_last>0");
      List simOrderList = new ArrayList();
      Iterator it = simList.iterator();
      try {
	while (it.hasNext()) {
	  OrderSumMain osm = (OrderSumMain) it.next();
	  List ossList = gsservicelocal.findOrderSumSub("where ordersumid=" +
	      osm.getId().intValue()+" order by processPos");
	  if(ossList==null||ossList.size()==0)
	    continue;
	  simOrderList.add(new CombinedOrderSumMainSimilay(osm, ossList,
	      com.tc.dispatch.
	      gsservice.action.GSSimilary.calcSimilary(parse.
	      parseSpec(p.getSpec()), parse.parseSpec(osm.getSpec()))));
	}
      }
      catch (AlgorithmException ae) {
	System.out.println(
	    "AlgorithmException is found in getSortedCombinedSimOrder:" +
	    ae.getMessage());
	return null;
      }
      Collections.sort(simOrderList, new OrderSimComp());
      return simOrderList;
    }
    catch (GSException gse) {
      System.out.println("GSException is found in getSortedCombinedSimOrder:" +
			 gse.getMessage());
      return null;
    }

  }

  public CostEstimateDetail getCostEstimateDetail(OrderSumMain osm, List ossList) {
    /**@todo Complete this method*/
    return ComponentFactory.getInstance().getComponentEntryList(osm, ossList);
  }

  public CostEstimateDetail getCostEstimateDetail(OrderSumMain osm, List ossList, ComponentFilter filter) {
    return ComponentFactory.getInstance().getComponentEntryList(osm, ossList,
	filter);
  }

  public List CostEstimateCopy(OrderSumMain osmTo, List ossListTo, OrderSumMain osmFrom, List ossListFrom) {
    /**@todo Complete this method*/
    return ca.estimateCost(osmTo, ossListTo, osmFrom, ossListFrom);
  }

  public List CostEstimateAdjust(OrderSumMain osm, List ossList, Adjust adj, List osmList, HashMap ossMap) {
    /**@todo Complete this method*/
    return ca.adjustCost(osm, ossList, adj, osmList, ossMap);
  }

  public List CostEstimateAdjust(OrderSumMain osm, List ossList, String type) {
    /**@todo Complete this method*/
    if (type.equals(Calclation.ALL_TYPE)) {
      return ca.adjustCostAll(osm, ossList);
    }
    else if(type.equals(Calclation.WITHOUT_PEIFANG))
    {
      return ca.adjustCostWithOutPF(osm,ossList);
    }
    else {
      return ca.adjustCostMin(osm, ossList);
    }
  }
  public List CostEstimateAdjust(OrderSumMain osm, List ossList, Adjust adj, List osmList) {
    /**@todo Complete this method*/
    return ca.adjustCost(osm,ossList,adj,osmList);
  }
  public void runGPA(OrderSumMain osm) {
    /**@todo Complete this method*/
    GSGPA gpa=new GSGPA(osm);
    gpa.run();

  }
  public void runBP(int type) {
    /**@todo Complete this method*/
      bp.initType(type);
      bp.initializeData();
      System.out.println("now  training ");
      bp.train();
      System.out.println("after   train ,now is saving");
      bp.save();
  }
  public int[] predicatorNum(String A, int type) {
    /**@todo Complete this method*/
      bp.initType(type);
      return bp.predicate(A);

  }

}
